<?php
	class admin_admin extends My_Controller
	{
		function admin_admin()
		{
			parent::__construct();
			$this->check_login();
			$this->permiss_admin();
			$this->load->library('Hash');
		}
		
		function index()
		{
			$data['title']			= 'Quản Lý Administrator';
			$data['admin_group']	= $this->db->get_where('admin_groups', array())->result_array();
			$this->load->library('pagination');
			$per_page 				= 30;		
			$total 	  				= $this->mod_lib->count('admins', array());
			$data['admin_admin']	= $this->mod_lib->load_all('admins', '', array(), $per_page, $this->uri->segment(4), array("id"=>"ASC"));
			$base_url				= site_url('admins/admin_admin/index/');		
			$config['base_url']		= $base_url;
			$config['per_page']		= $per_page;
			$config['total_rows']	= $total;		
			$config['uri_segment']	= 4;
			$data['rs']				= $this->uri->segment(4);	
			$this->pagination->initialize($config);		
			$data['paging']			= $this->pagination->create_links();
			$data['content_admin']	= 'admins/admin_admin/index';
			$this->load->view('admins/template_admin', $data);			
		}
		
		function add()
		{
			$i = $this->permiss_admin_add();				
			if(isset($i['all']) || isset($i['add']))
			{							
				$data['title']				= 'Thêm Mới Administrator';
				$data['group_admin']		= $this->db->get('admin_groups')->result_array();
				if ($this->input->post('cmd'))
				{
					$username		= trim($this->input->post('username'));
					$data			= array(
										'username'			=> trim($this->input->post('username')),
										'password'			=> md5('cdraovat'.md5($this->input->post('password'))),
										'fullname'			=> $this->input->post('fullname'),
										'email'				=> $this->input->post('email'),
										'phone'				=> $this->input->post('phone'),
										'status'			=> $this->input->post('status'),
										'admin_group_id'	=> $this->input->post('admin_group')
										);
					$a 	= $this->db->get_where('admins', array('username'=>$this->input->post('username')));
					if ($a->num_rows() > 0)
					{
						$_SESSION['error_add']	= 'Tài khoản này đã tồn tại. Vui lòng chọn tài khoản khác';
						redirect(base_url().'admins/admin_admin/add');
					}
					else 
					{
						$_SESSION['error_add']	= '';
						$this->db->insert('admins', $data);
						$hash_user 				= $this->hash->create($username, '', 'md5sha512');
						$path_user				= 'upload/'.$hash_user;
						$path_products			= $path_user.'/product';
						if (!file_exists($path_user))
						{
							mkdir($path_user, 0777);
						}
						if (!file_exists($path_products))
						{
							mkdir($path_products, 0777);
						}
						$action = 'Thêm mới admin: '.$this->input->post('username');
						$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
						if ($this->input->post('cmd')	== 'Add')
						{
							echo '<script>alert("Bạn đã thêm mới thành công."); window.location = "'.base_url().'admins/admin_admin"</script>';
						}
						else if ($this->input->post('cmd')	== 'Save & Add')
						{
							redirect(base_url().'admins/admin_admin/add');
						}
						//redirect(base_url().'admins/admin_admin');
					}
				}
				$data['content_admin']	= 'admins/admin_admin/add';
				$this->load->view('admins/template_admin', $data);
			}
			else
			{
				$_SESSION['permiss_error'] = "Bạn không có quyền thêm admin";
				redirect(base_url()."admins/admin_admin");	
			}
		}
		
		function edit($id)
		{
			$i = $this->permiss_admin_add();
			if(isset($i['all']) || isset($i['edit']))
			{	
				$data['title']			= 'Sửa Thông Tin Administrator';
				$data['group_admin']	= $this->db->get('admin_groups')->result_array();
				$data['admin_edit']		= $this->db->get_where('admins', array('id'=>$id))->result_array();
				$pass					= $this->db->get_where('admins', array('id'=>$id))->result_array();
				$name					= $pass[0]['username'];
				
				if ($this->input->post('cmd'))
				{
					$password_old			= $this->input->post('password_old');
					if (!empty($password_old))
					{
						$password_old				= md5('cdraovat'.md5($password_old));
						if ($pass[0]['password']	== $password_old)
						{
							$data			= array(
													'username'			=> trim($this->input->post('username')),
													'password'			=> md5('cdraovat'.md5($this->input->post('password'))),
													'fullname'			=> $this->input->post('fullname'),
													'email'				=> $this->input->post('email'),
													'phone'				=> $this->input->post('phone'),
													'status'			=> $this->input->post('status'),
													'admin_group_id'	=> $this->input->post('admin_group')
										);
							$this->mod_lib->update('admins', array('id'=>$id), $data);
							$_SESSION['error_edit']	= '';
							$action		= 'Sửa admin: '.$name.' thành '.$this->input->post('username');
							$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
							redirect(base_url().'admins/admin_admin');
						}
						else
						{
							$_SESSION['error_edit']	= 'Password cũ không hợp lệ !';
							redirect(base_url().'admins/admin_admin/edit/'.$id);
						}
					}
					else 
					{
						$data			= array(
											'username'			=> trim($this->input->post('username')),
											'fullname'			=> $this->input->post('fullname'),
											'email'				=> $this->input->post('email'),
											'phone'				=> $this->input->post('phone'),
											'status'			=> $this->input->post('status'),
											'admin_group_id'	=> $this->input->post('group_admin')
										);
						$this->mod_lib->update('admins', array('id'=>$id), $data);
						$_SESSION['error_edit']	= '';
						$action		= 'Sửa admin '.$name.' thành '.$this->input->post('username');
						$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
						redirect(base_url().'admins/admin_admin');
					}
				}
				$data['content_admin']	= 'admins/admin_admin/edit';
				$this->load->view('admins/template_admin', $data);
			}
			else
			{
				$_SESSION['permiss_error'] = "Bạn không có quyền sửa admin";
				redirect(base_url().'admins/admin_admin');
			}
		}
		
		function delete()
		{
			$i = $this->permiss_admin_add();			
			if(isset($i['all']) || isset($i['del']))
			{
				$id			   = $this->input->post('chon');			
				$idss		   = substr($id, 0, strlen($id)-1);		
				$this->db->where_in('admin_id', $idss);
				$permiss	= $this->db->get('admin_permiss');
				if($permiss->num_rows() > 0)
				{
					foreach($permiss->result_array() as $per)
					{
						$this->mod_lib->delete('admin_permiss', $per['admin_id'], 'admin_id');
					}
				}						
				$id			= substr($id, 0, strlen($id)-1);
				$del		= explode(',', $idss);
				$id_del		= "";
				foreach ($del as $delete)
				{
					if ($delete != 1)
					{
						$id_del.= $delete.',';
					}
				}
				
				$id_del		= substr($id_del, 0, strlen($id_del)-1);
				$mang_id    = explode(',', $id_del);
				$action     = array();
				foreach ($mang_id as $id)
				{
					$dat 		= $this->mod_lib->detail('admins', array('id'=>$id));
					$action[] 	= $dat[0]['username'];
				}

				$hd = '';
				foreach ($action as $ac)
				{
					if ($hd	== "")
					{
						$hd .= 'Xóa admin: '.$ac;
					}
					else
					{
						$hd .= ', '.$ac;
					}
				}
				$this->mod_lib->delete('admins', $mang_id, 'id', true);
				$this->mod_lib->insert_log($hd, $_SESSION["admin_id"]);
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = "Bạn không có quyền xóa";
				redirect(base_url()."admins/admin_admin");
			}
		}
		
		function del($id)
		{
			$i = $this->permiss_admin_add();			
			if(isset($i['all']) || isset($i['del']))
			{
				if ($id != 1 || $id != 2)
				{
					$this->mod_lib->delete('admins', $id, 'id');
					$this->mod_lib->delete('admin_permiss', $id, 'admin_id');
				}
				redirect($_SESSION["last_url_admin"]);				
			}
			else
			{
				$_SESSION['permiss_error'] = "Bạn không có quyền xóa admin";
				redirect(base_url()."admins/admin_admin");
			}
		}
		
		function active($id, $status)
		{
			$i = $this->permiss_admin_add();
			if(isset($i['all']) || isset($i['edit']))
			{
				if ($status == 1)
				{
					$update_status = 0;
				}
				else
				{
					$update_status = 1;
				}
				$data 		= array('status' =>$update_status);			
				$this->mod_lib->update('admins', array("id"=>$id), $data);
				$query		= $this->db->get_where('admins', array('id'=>$id))->row_array();
				$username	= $query['username'];
				$action		= 'Thay đổi trạng thái tài khoản Admin: '.$username;
				$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền thay đổi trạng thái admin';
				redirect($_SESSION['last_url_admin']);
			}
		}
		
		function permiss($id, $user)
		{
			//$query_admins		= $this->mod_lib->detail('admins', array('id'=>$id));
			//$group_id			= $query_admins[0]['admin_group_id'];
			//$query_permiss		= $this->mod_lib->detail('admin_permiss', array('group_admin_id'=>$group_id));
			/*
			foreach ($query_permiss as $value)
			{
				echo $value['menu_admin_id'].', ';
				$this->db->where_in('id', $value);
				$data['menu']		= $this->db->get_where('admin_menu', array('cat_id !='=>0))->result_array();
			}
			*/
			$data['title'] 		= 'Phân quyền thành viên';
			$query_admin		= $this->mod_lib->detail('admins', array('id'=>$id));
			$group_id			= $query_admin[0]['admin_group_id'];
			$query_permiss		= $this->mod_lib->detail('admin_permiss', array('group_admin_id'=>$group_id, 'access'=>1));
			if (!empty($query_permiss))
			{
				$menu_admin_id		= array();
				foreach ($query_permiss as $value_menu_id)
				{
					$menu_admin_id[]	= $value_menu_id['menu_admin_id'];
				}
				$a	= '';
				foreach ($menu_admin_id as $m)
				{
					$a	.= $m.',';
				}
				$b	= substr($a, 0, strlen($a)-1);
				$sql	= "SELECT * FROM cd_admin_menu WHERE cat_id IN ($b) AND status = 1";
				$query	= $this->db->query($sql);
				$data['menu']	= $query->result_array();
			}
			//$this->db->distinct();
			//$data['menu']    	= $this->mod_lib->join('admin_menu, admins', 'admin_menu.name, admin_menu.id', array('cat_id !='=>0, 'admins.admin_group_id'=>$group_id, 'admin_permiss.admin_id'=>$id), array('admin_permiss'=>'admin_permiss.menu_admin_id = admin_menu.id', 'admin_groups'=>'admin_groups.id = admin_permiss.group_admin_id'), '', '', array());
			//$data['menu']		= $this->db->get_where('admin_menu', array('cat_id !='=>0))->result_array();
			//echo $this->db->last_query();
			if($this->input->post('cmd'))
			{
				foreach ($data['menu'] as $menu)
				{
					$admin_permiss_id	= $menu['id'];
					$Per 	   = 0;
					$Post 	   = 0;
					$Edit 	   = 0;
					$Del 	   = 0;
														
					if($this->input->post("Per_".$admin_permiss_id) == 1)
					{
						$Per 		= $this->input->post("Per_".$admin_permiss_id); 
						if($this->input->post("Post_".$admin_permiss_id)==1) 
						{
							$Post 	= $this->input->post("Post_".$admin_permiss_id);		
						}
						if($this->input->post("Edit_".$admin_permiss_id)==1)
						{
							$Edit 	= $this->input->post("Edit_".$admin_permiss_id);		
						}
						if($this->input->post("Del_".$admin_permiss_id)==1)
						{
							$Del 	= $this->input->post("Del_".$admin_permiss_id);	
						}
					}
					
					$searchMenu3InAdminPermis = $this->db->get_where('admin_permiss', array('menu_admin_id' => $admin_permiss_id, 'admin_id' => $id));
					if($searchMenu3InAdminPermis->num_rows() > 0)
					{
						$dataWhere = array('menu_admin_id' 	=> $admin_permiss_id, 
										   'admin_id' 		=> $id
											
						);
									
						$dataInput = array('access' 		 => $Per, 
										   'add' 			 => $Post, 
										   'edit'  			 => $Edit, 
										   'del' 			 => $Del
						);
						$this->mod_lib->update('admin_permiss', $dataWhere, $dataInput);
					}
					else
					{
						$dataInput = array('menu_admin_id' 	=> $admin_permiss_id, 
										   'admin_id'		=> $id,
										   'access' 		=> $Per, 
										   'add' 			=> $Post, 
										   'edit'  			=> $Edit, 
										   'del' 			=> $Del
						);
						$this->db->insert('admin_permiss', $dataInput);
					}
				}
				redirect(base_url().'admins/admin_admin/permiss/'.$id.'/'.$user);
			}
			$data['admin_id']		= $id;
			$data['admin_user']		= $user;
			$data['content_admin']	= 'admins/admin_admin/admin_permiss/load_menu_3';
			$this->load->view('admins/template_admin',$data);
		}
		
		function permissMenu2($id, $name = "")
		{
			$data['title']		= 'Phân Quyền Cho Admin';					
			$data['admin_id']	= $id;
			$data['admi_name']	= $name;
			$admin           	= $this->db->get_where('admins', array('id' => $id));//lay dc admin_group thong qua admin_id
			if($admin->num_rows() > 0)
			{				
				$group_id 	   = $admin->row_array();										
				$admin_permiss = $this->db->get_where('admin_permiss', array('group_admin_id' => $group_id['admin_group_id'], 'access' => '1'));				
				if($admin_permiss->num_rows() > 0)
				{					
					$data['admin_menu_1'] = $admin_permiss;								
				}				
			}						
			$data['content_admin']		= 'admins/admin_admin/admin_permiss/index';
			$this->load->view('admins/template_admin', $data);
		}
		
		function permissMenu3($menu_2_id, $admin_id)
		{			
			$data['title']		= 'Phân quyền Admin';
			$data['admin_id']  = $admin_id;
			$data['menu_id_2'] = $menu_2_id;			
			$data["menu_3"]    = $this->db->get_where('admin_menu', array('cat_id !=' => 0))->result_array();
			if($this->input->post('cmd'))
			{								
				foreach ($data['menu_3'] as $menu)
				{
					$admin_permiss_id	= $menu['id'];
					$Per 	   = 0;
					$Post 	   = 0;
					$Edit 	   = 0;
					$Del 	   = 0;
														
					$menu_id_3 = $this->input->post('menu_id_3_'.$admin_permiss_id);					
					if($this->input->post("Per_".$admin_permiss_id) == 1)
					{
						$Per 		= $this->input->post("Per_".$admin_permiss_id); 
						if($this->input->post("Post_".$admin_permiss_id)==1) 
						{
							$Post 	= $this->input->post("Post_".$admin_permiss_id);		
						}
						if($this->input->post("Edit_".$admin_permiss_id)==1)
						{
							$Edit 	= $this->input->post("Edit_".$admin_permiss_id);		
						}
						if($this->input->post("Del_".$admin_permiss_id)==1)
						{
							$Del 	= $this->input->post("Del_".$admin_permiss_id);	
						}
					}
					
					$searchMenu3InAdminPermis = $this->db->get_where('admin_permiss', array('menu_admin_id' => $menu_id_3, 'admin_id' => $admin_id));
					if($searchMenu3InAdminPermis->num_rows() > 0)
					{
						$dataWhere = array('menu_admin_id' 	=> $menu_id_3, 
										   'admin_id' 		=> $admin_id
						);
									
						$dataInput = array('access' 		 => $Per, 
										   'add' 			 => $Post, 
										   'edit'  			 => $Edit, 
										   'del' 			 => $Del
						);
						$this->mod_lib->update('admin_permiss', $dataWhere, $dataInput);
					}
					else
					{
						$dataInput = array('menu_admin_id' 	=> $menu_id_3, 
										   'admin_id'		=> $admin_id,
										   'access' 		=> $Per, 
										   'add' 			=> $Post, 
										   'edit'  			=> $Edit, 
										   'del' 			=> $Del
						);
						$this->db->insert('admin_permiss', $dataInput);
					}										
				}
				redirect(base_url().'admins/admin_admin/permissMenu2/'.$admin_id);						
			}			
			$data['content_admin']	= 'admins/admin_admin/admin_permiss/load_menu_3';	
			$this->load->view('admins/template_admin', $data);
		}
	}